package com.qys.downservlet;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

@WebServlet("/down")
@MultipartConfig
public class downservlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		LocalDate date = LocalDate.now();
		int month = date.getMonthValue();
		int today = date.getDayOfMonth();
		String path = "E:\\C\\"+month+"月"+today+"日";
		File file = new File(path);
		File[] files = file.listFiles();
		String filename = files[0].getName();
		double filesize = files[0].length();
		System.out.println("文件的名字："+filename);
		resp.addHeader("content-disposition", "attachment;filename="+filename);//获取文件头信息
		FileUtils.copyFile(files[0], resp.getOutputStream());//将上面从下载源路径中获取的一个文件复制进响应地址。
		System.out.println("有客户端接入");

	  } 
	}

